﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Flame.Dlr;
using System.IO;
using System.Threading;

namespace Flame.Controls
{
    public partial class OutputStreamWindow : Form
    {

        Type _lastLanguageInput = Languages.None;

        public OutputStreamWindow(Manager compilerManager)
        {
            InitializeComponent();

            compilerManager.OnOutput += new EventHandler<StreamOutput.StreamOutputEventArg>(compilerManager_OnOutput);
            compilerManager.OnError += new EventHandler<ErrorEventArg>(compilerManager_OnError);
            compilerManager.OnInput += new EventHandler<InputEventArg>(compilerManager_OnInput);
            compilerManager.OnResult += new EventHandler<ResultEventArg>(compilerManager_OnResult);
        }

        void compilerManager_OnResult(object sender, ResultEventArg e)
        {
            text.SelectionColor = Color.Black;
            string[] ss = e.Result.Data.ToString().Split('\r');
            if(ss.Length == 1)
                text.AppendText("-> " + ss[0]);
            else if (ss.Length > 1)
                text.AppendText("-> " + ss[0] + " [..]");
            text.AppendText(Environment.NewLine);
            scrolltoend();
        }

        void compilerManager_OnInput(object sender, InputEventArg e)
        {
            _lastLanguageInput = e.Language;
        }

        void compilerManager_OnError(object sender, ErrorEventArg e)
        {
            text.SelectionColor = Color.Red;
            text.AppendText(e.Error.Message);
            text.AppendText(Environment.NewLine);
            scrolltoend();
        }

        void scrolltoend()
        {
            text.SelectionStart = text.Text.Length;
            text.ScrollToCaret();
        }


        void add(string stext)
        {
            text.SelectionColor = _lastLanguageInput.Name == "IronPythonExec" ? Color.DarkGreen : Color.DarkRed;


            //text.AppendText(">> " + e.Input.Replace(Environment.NewLine, Environment.NewLine + ">> "));
            //text.AppendText(Environment.NewLine);
            text.SelectionColor = Color.Black;
            text.AppendText(stext);
            /*string[] ss = e.Result.Split('\r');
            if(ss.Length == 1)
                text.AppendText("-> " + ss[0]);
            else if (ss.Length > 1)
                text.AppendText("-> " + ss[0] + " [..]");*/
            //text.AppendText(Environment.NewLine);
            scrolltoend();
        }

        void compilerManager_OnOutput(object sender, StreamOutput.StreamOutputEventArg e)
        {
            //uuu//
            if (text.Invoke(new Func<Thread>(() => { return Thread.CurrentThread; })) != Thread.CurrentThread)
                text.BeginInvoke(new Action(() => { add(e.Output); }));
            else
                add(e.Output);
        }
        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            text.Text = "";
        }


        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            Hide();
            e.Cancel = true;
        }


        private void saveToTxtToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog();

            sf.Filter = "(*.txt)|*.txt";
            DialogResult d = sf.ShowDialog();
            if (d == DialogResult.OK)
            {
                File.AppendAllText(sf.FileName, text.Text);
            }
        }

        private void saveToRtfToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog();

            sf.Filter = "(*.rtf)|*.rtf";
            DialogResult d = sf.ShowDialog();
            if (d == DialogResult.OK)
            {
                text.SaveFile(sf.FileName);
            }
        }


    }
}
